package misscl.online.slh_gym.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import misscl.online.slh_gym.entity.Coach;
import misscl.online.slh_gym.entity.CoachCourse;
import misscl.online.slh_gym.entity.CoachVo;
import misscl.online.slh_gym.entity.Course;
import misscl.online.slh_gym.service.CoachCourseService;
import misscl.online.slh_gym.service.CoachService;
import misscl.online.slh_gym.service.CourseService;
import misscl.online.slh_gym.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author theo
 * @since 2022/11/28 22:03
 */
@RestController
@RequestMapping("/coach")
public class CoachController {


    @Autowired
    private CoachService coachService;
    @Autowired
    private CoachCourseService coachCourseService;
    @Autowired
    private CourseService courseService;
    @GetMapping("/list")
    public AjaxResult list(){
        List<Coach> list = coachService.list();
        Map<Integer,List<Course>> map = new HashMap<>();
        ArrayList<CoachVo> coachVos = new ArrayList<>();
        for (Coach coach : list) {
            CoachVo coachVo = new CoachVo();
            coachVo.setCoachAge(coach.getCoachAge());
            coachVo.setSalary(coach.getSalary());
            coachVo.setStatus(coach.getSalary());
            coachVo.setCreateTime(coach.getCreateTime());
            coachVo.setAddress(coach.getAddress());
            coachVo.setSex(coach.getSex());
            coachVo.setPhone(coach.getPhone());
            coachVo.setName(coach.getName());
            coachVo.setId(coach.getId());
            QueryWrapper<CoachCourse> wrapper = new QueryWrapper<>();
            QueryWrapper<CoachCourse> coach_id = wrapper.eq("coach_id", coach.getId());
            List<CoachCourse> list1 = coachCourseService.list(coach_id);
            List<Course> list3 = new ArrayList<>();
            for (CoachCourse coachCourse : list1) {
                QueryWrapper<Course> wrapper1 = new QueryWrapper<>();
                QueryWrapper<Course> course = wrapper1.eq("id", coachCourse.getCourseId());
                List<Course> list2 = courseService.list(course);
                list3.add(list2.get(0));
            }
            coachVo.setCourseList(list3);
            coachVos.add(coachVo);
//            map.put(coach.getId(),list3);
        }
        return AjaxResult.ok("教练列表获取成功,sex:0男，1女").data("coachList",coachVos);

    }


}
